<?php

class Member_jiangjinController extends Member {
    public $jiangjin;
	public $member;
	
    public function __construct() {
		parent::__construct();
		$this->jiangjin = $this->model('jiangjin');
		$this->member = $this->model('member');
		
	}
	
	public function indexAction() {
		
		if(!get_cookie('openid')){
		 $this->oauthlogin();//微信授权跟获取
		}
	
		$member_id = get_cookie('yuangong_id');//判断是否已经登录
		if(empty($member_id)){
			$back = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : url('member/');
			$this->redirect(url('member/', array('back' => urlencode($back))));
		}
		$type = get_cookie('type');
		if($type){
			$this->redirect(url('member/'));
		}
		$member = $this->db->where('id',$member_id)->get('member')->row_array();
		//print_r($member);
		$where = '1=1 and member_id='.$member_id;
		
		$page = (int)$this->get('page') ? (int)$this->get('page') : 1;
	    //$pagesize = isset($this->site['SITE_ADMIN_PAGESIZE']) && $this->site['SITE_ADMIN_PAGESIZE'] ? $this->site['SITE_ADMIN_PAGESIZE'] : 8;
		
		
		
		$pagesize = 10;
	    $urlparam = array('page' => '{page}');
		$order = 'addtime desc';
		$limit = ($page-1)*$pagesize.",$pagesize";
		
		$data =  $this->jiangjin->get_all_list($where,$order ,$limit); //列表
		$total = $this->jiangjin->get_all_list($where,$order ,$limit,true); //统计数量	

		$count = array();	//统计各个状态的数据量
	    $pagelist = $this->instance('pagelist');	//加载分页类
		$pagelist->loadconfig();
		$pagelist = $pagelist->total($total)->url(url('member/member_jiangjin/', $urlparam))->num($pagesize)->page($page)->output();
		$backurl = $this->get('back') ? $this->get('back') : (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : url('member/'));
		$pagenum = ceil($total/$pagesize);
		$jjgz = isset($this->site['SITE_ADMIN_JIANGLI']) && $this->site['SITE_ADMIN_JIANGLI'] ? $this->site['SITE_ADMIN_JIANGLI'] : 1;//奖金规则%
		
		$this->view->assign(array(
			'indexc' => 'yongjin',
			'jjgz' => $jjgz,
			'member' => $member,
			'pagenum' => $pagenum,
			'meta_title' => lang('奖励明细'),
	        'page' => $page,
	        'list' => $data,
			'total' => $total,
	        'pagelist' => $pagelist
	    ));
		$this->view->display('member/member_jiangjin');
	}
	
	
	
	public function monthjiangjinAction(){//月奖金-自动执行
	//echo strtotime('2016-12');
		$on = $this->site['SITE_ADMIN_JJKQ'];
		if(!$on){
			return false;
		}
		$list = $this->member->get_all_num();
		$count = count($list);
		//print_r($count);
		
		for($i=0;$i<$count;$i++){
			$addtime = time();
			//第一步，添加当日余额
			$this->member->addrijun($list[$i]['id'],$list[$i]['yue'],$addtime);
			
			//第二步，把当日余额添加到，当月余额总额中
			$yuejun_id = $this->member->getyuejun($list[$i]['id'],$addtime);
			if($yuejun_id){
				$this->member->updateyuejun($yuejun_id,$list[$i]['yue'],$addtime);
			}else{
				$this->member->addyuejun($list[$i]['id'],$list[$i]['yue'],$addtime);
			}
			
			//第三步，判断是否为当月最后一天，添加当月个人奖金总额
			if(date('d',$addtime) == date('t',$addtime)){
					$jjgz = isset($this->site['SITE_ADMIN_JIANGLI']) && $this->site['SITE_ADMIN_JIANGLI'] ? $this->site['SITE_ADMIN_JIANGLI'] : 1;
					$this->member->addjiangjin($list[$i]['id'],$addtime,$jjgz);
			}
		}
		
	}
	
	/*public function ceshiAction(){//日测试
		$riqi = $this->get('riqi');
		$addtime = strtotime($riqi);
		$list = $this->member->get_all_num();
		$count = count($list);
		//print_r($count);
		for($i=0;$i<$count;$i++){
			//第一步，添加当日余额
			$this->member->addrijun($list[$i]['id'],$list[$i]['yue'],$addtime);
			
			//第二步，把当日余额添加到，当月余额总额中
			$yuejun_id = $this->member->getyuejun($list[$i]['id'],$addtime);
			if($yuejun_id){
				$this->member->updateyuejun($yuejun_id,$list[$i]['yue'],$addtime);
			}else{
				$this->member->addyuejun($list[$i]['id'],$list[$i]['yue'],$addtime);
			}
			//第三步，判断是否为当月最后一天，添加当月个人奖金总额
			if(date('d',$addtime) == date('t',$addtime)){
					$this->member->addjiangjin($list[$i]['id'],$addtime);
			}
			
		}
		if(date('d',$addtime) == date('t',$addtime)){
			echo "今天是".date('Y年m月',$addtime)."最后一天，会自动增加当月奖金到会员奖励中<br />请到后台的【会员管理】下的》奖励&nbsp;&nbsp;&nbsp;查看当月奖金<br /><br />";
		}
		echo "统计".date("Y年m月d日",$addtime)."所有会员当天余额<br /><br />";
		foreach($list as $v){
			$tongji = $this->member->getdangyuecis($v['id'],$addtime);
			echo "会员名称：【".$v['name']."】当天余额：".$v['yue']."&nbsp;&nbsp;&nbsp;已统计天数：".$tongji['tianshu']."&nbsp;&nbsp;&nbsp;统计总金额：".$tongji['zonge']."&nbsp;&nbsp;&nbsp;"."
			每月奖金计算方式：(当月消费+(".$tongji['zonge']."/".$tongji['tianshu']."))X0.01&nbsp;&nbsp;<br /><br />";
		}
	}*/

	
	

	
}